package com.lagou.server;

import java.io.IOException;
import java.io.InputStream;

/**
 * 根据InputStream封装请求信息
 */
public class Request {

    // 请求方式
    private String method;

    // 请求地址
    private String url;

    // 输入流，其他属性从输入流中解析出来
    private InputStream inputStream;

    public Request() {

    }

    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;

        // 从输入流中获取请求信息
        int available = 0;
        while (available == 0) {
            available = inputStream.available();
        }

        byte[] bytes = new byte[available];

        inputStream.read(bytes);

        String inputString = new String(bytes);

        String firstLineStr = inputString.split("\\n")[0];

        String[] strings = firstLineStr.split(" ");

        this.method = strings[0];
        this.url = strings[1];

        System.out.println("method: " + this.method + "; url: " + this.url);

    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }
}
